package com.eascs.user.web.${model.lowerName}.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ${model.packageName}.api.bean.request.${model.initUpperName}CreateRequest;
import ${model.packageName}.api.bean.request.${model.initUpperName}UpdateRequest;
import ${model.packageName}.api.bean.request.${model.initUpperName}QueryRequest;
import ${model.packageName}.api.bean.request.${model.initUpperName}DeleteRequest;
import ${model.packageName}.api.bean.request.${model.initUpperName}PageRequest;
import ${model.packageName}.api.bean.response.${model.initUpperName}InfoResponse;
import ${model.packageName}.api.facade.${model.initUpperName}ReadFacade;
import ${model.packageName}.api.facade.${model.initUpperName}WriteFacade;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.draco.web.autoconfig.exception.UcRestException;
import io.terminus.draco.web.common.extension.WebApi;
import io.terminus.parana.exception.RestException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wang.xi
 */
@Slf4j
@RestController
@Api(
	value = "${model.desc}_Controller",
	tags = {"${model.desc}_Controller"}
)
@RequestMapping("/api/user/web/${model.initLowerName}")
@RequiredArgsConstructor
public class ${model.initUpperName}Controller {

    private final ${model.initUpperName}ReadFacade ${model.initLowerName}ReadFacade;
    private final ${model.initUpperName}WriteFacade ${model.initLowerName}WriteFacade;
    
    @WebApi
    @PostMapping(value = "/create")
    @ApiOperation("创建${model.desc}")
    public Boolean create(@RequestBody ${model.initUpperName}CreateRequest request,
                                             HttpServletRequest servletRequest,
                                             HttpServletResponse servletResponse) {
    	
        Response<Boolean> response = ${model.initLowerName}WriteFacade.create${model.initUpperName}(request);
        if (!response.isSuccess()) {
            log.info("create ${model.initUpperName} failed, params:{}, cause:{}",
                    request, response.getError());
            throw new UcRestException(response.getError(), response.getCode(), response.getArgs());
        }
        return response.getResult();
    }
    
    @WebApi
    @PostMapping(value = "/update")
    @ApiOperation("修改${model.desc}")
    public Boolean update(@RequestBody ${model.initUpperName}UpdateRequest request,
                                             HttpServletRequest servletRequest,
                                             HttpServletResponse servletResponse) {
    	
        Response<Boolean> response = ${model.initLowerName}WriteFacade.update${model.initUpperName}(request);
        if (!response.isSuccess()) {
            log.info("update ${model.initUpperName} failed, params:{}, cause:{}",
                    request, response.getError());
            throw new UcRestException(response.getError(), response.getCode(), response.getArgs());
        }
        return response.getResult();
    }
    
    @WebApi
    @PostMapping(value = "/delete")
    @ApiOperation("删除${model.desc}")
    public Boolean delete(@RequestBody ${model.initUpperName}DeleteRequest request,
                                             HttpServletRequest servletRequest,
                                             HttpServletResponse servletResponse) {
    	
        Response<Boolean> response = ${model.initLowerName}WriteFacade.delete${model.initUpperName}(request);
        if (!response.isSuccess()) {
            log.info("delete ${model.initUpperName} failed, params:{}, cause:{}",
                    request, response.getError());
            throw new UcRestException(response.getError(), response.getCode(), response.getArgs());
        }
        return response.getResult();
    }
    
    @WebApi
    @GetMapping(value = "/view")
    @ApiOperation("查询${model.desc}")
    public ${model.initUpperName}InfoResponse view(${model.initUpperName}QueryRequest request, 
    										HttpServletRequest servletRequest,
                                            HttpServletResponse servletResponse) {
    	
    	Response<${model.initUpperName}InfoResponse> response = ${model.initLowerName}ReadFacade.view${model.initUpperName}(request);
        if (!response.isSuccess()) {
            log.info("view ${model.initUpperName} failed, params:{}, cause:{}",
                    request, response.getError());
            throw new UcRestException(response.getError(), response.getCode(), response.getArgs());
        }
        return response.getResult();
    }
    
    @WebApi
    @GetMapping(value = "/list")
    @ApiOperation("查询${model.desc}列表")
    public List<${model.initUpperName}InfoResponse> list(${model.initUpperName}QueryRequest request,
    										HttpServletRequest servletRequest,
                                            HttpServletResponse servletResponse) {
    	
    	Response<List<${model.initUpperName}InfoResponse>> response = ${model.initLowerName}ReadFacade.list${model.initUpperName}(request);
        if (!response.isSuccess()) {
            log.info("list ${model.initUpperName} failed, params:{}, cause:{}",
                    request, response.getError());
            throw new UcRestException(response.getError(), response.getCode(), response.getArgs());
        }
        return response.getResult();
    }
    
    @WebApi
    @ApiOperation("分页查询${model.desc}列表")
    @GetMapping({"/page"})
    public Paging<${model.initUpperName}InfoResponse> page(${model.initUpperName}PageRequest request,
    										HttpServletRequest servletRequest,
                                            HttpServletResponse servletResponse) {
                                            
        Response<Paging<${model.initUpperName}InfoResponse>> response = ${model.initLowerName}ReadFacade.page${model.initUpperName}(request);
        if (!response.isSuccess()) {
            throw new RestException(response.getError());
        }
        return response.getResult();
    }
}
